package com.sk.sourcecircle.activity.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.adapter.CircleFriendAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.CircleFriendList;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/1/21.
 */

public class GroupPepActivity extends BaseActivity implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.pep_ryc)
    RecyclerView pepRyc;
    private CircleFriendAdapter adapter;
    private LoadingDialog loadingDialog;
    private List<CircleFriendList.DataBean> allDataList = new ArrayList<>();

    private String ci_id;
    private String name;

    @Override
    protected int getContentView() {
        return R.layout.activity_group_pep;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ci_id = getIntent().getStringExtra("id");
        name = getIntent().getStringExtra("name");

        pepRyc.setLayoutManager(new GridLayoutManager(mContext, 4));
        adapter = new CircleFriendAdapter(R.layout.item_circle_friend, allDataList);
        pepRyc.setAdapter(adapter);

        View head = LayoutInflater.from(mContext).inflate(R.layout.group_head, pepRyc, false);
        TextView textView = head.findViewById(R.id.circle_name);
        textView.setText(name);
        adapter.addHeaderView(head);

        adapter.setOnItemClickListener(this);


        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        loadingDialog.show();
        AppApi.circleFriends(ci_id, this, 0, this);


    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        CircleFriendList circleFriendList = JSON.parseObject(result, CircleFriendList.class);
        List<CircleFriendList.DataBean> dataBeanList = circleFriendList.getData();
        allDataList.clear();
        allDataList.addAll(dataBeanList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, PersonDetailsActivity.class);
        intent.putExtra("id", allDataList.get(position).getUser_id());
        startActivity(intent);
    }
}
